package com.tqjc.system.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tqjc.system.common.constant.AddTaskConstant;
import com.tqjc.system.common.constant.GlobalConstant;
import com.tqjc.system.common.constant.SQLConstant;
import com.tqjc.system.common.entity.VO.TaskInfoVO;
import com.tqjc.system.common.enums.TaskStatusEnum;
import com.tqjc.system.common.enums.TaskTypeEnum;
import com.tqjc.system.system.dao.SysAddPrimaryTaskDao;
import com.tqjc.system.system.entity.SysAddPrimaryTaskEntity;
import com.tqjc.system.system.service.SysAddPrimaryTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author 86195
 * @version 1.0
 * @description
 * @date 2023/10/16 15:14
 */
@Service
@Slf4j
public class SysAddPrimaryTaskServiceImpl extends ServiceImpl<SysAddPrimaryTaskDao, SysAddPrimaryTaskEntity> implements SysAddPrimaryTaskService {
    @Override
    public void complete(String slotNo, String status) {
        baseMapper.update(null,new LambdaUpdateWrapper<SysAddPrimaryTaskEntity>()
                .set(SysAddPrimaryTaskEntity::getStatus,status).set(SysAddPrimaryTaskEntity::getCompleteDate,new Date())
                .eq(SysAddPrimaryTaskEntity::getStatus, TaskStatusEnum.STATUS_DOING.getStatus())
                .eq(SysAddPrimaryTaskEntity::getTankCarNo,slotNo));
    }

    @Override
    public SysAddPrimaryTaskEntity selectDoingTask(String slotNo) {
        return baseMapper.selectOne(new LambdaQueryWrapper<SysAddPrimaryTaskEntity>()
                .eq(SysAddPrimaryTaskEntity::getTankCarNo, slotNo)
                .eq(SysAddPrimaryTaskEntity::getStatus, TaskStatusEnum.STATUS_DOING.getStatus()).last(SQLConstant.SQL_LIMIT_1));
    }

    public SysAddPrimaryTaskEntity getModifyTask(String slotNo){
        return baseMapper.selectOne(new LambdaQueryWrapper<SysAddPrimaryTaskEntity>()
                .eq(SysAddPrimaryTaskEntity::getTankCarNo, slotNo)
                .in(SysAddPrimaryTaskEntity::getStatus, TaskStatusEnum.STATUS_DOING.getStatus(),TaskStatusEnum.STATUS_UNDOING.getStatus()).last(SQLConstant.SQL_LIMIT_1));
    }

    @Override
    public void updateNum(String idKey) {
        baseMapper.updateNum(idKey);
    }

    @Override
    public IPage<SysAddPrimaryTaskEntity> selectListByPage(Page<SysAddPrimaryTaskEntity> pageEntity, TaskInfoVO taskInfoVO) {
        LambdaQueryWrapper<SysAddPrimaryTaskEntity> queryWrapper = new LambdaQueryWrapper<SysAddPrimaryTaskEntity>()
                .eq(StringUtils.isNotEmpty(taskInfoVO.getTankCarNO()),SysAddPrimaryTaskEntity::getTankCarNo, taskInfoVO.getTankCarNO())
                .le(taskInfoVO.getComEndDate() != null, SysAddPrimaryTaskEntity::getCompleteDate, taskInfoVO.getComEndDate())
                .ge(taskInfoVO.getComStartDate() != null, SysAddPrimaryTaskEntity::getCompleteDate, taskInfoVO.getComStartDate())
                .le(taskInfoVO.getDistEndDate() != null, SysAddPrimaryTaskEntity::getDistributeDate, taskInfoVO.getDistEndDate())
                .ge(taskInfoVO.getDistStartDate() != null, SysAddPrimaryTaskEntity::getDistributeDate, taskInfoVO.getDistStartDate())
                .orderByDesc(SysAddPrimaryTaskEntity::getCreateDate);
        return super.page(pageEntity,queryWrapper);
    }

    @Override
    public void addOpt(String tankCarNo) {
        SysAddPrimaryTaskEntity entity = SysAddPrimaryTaskEntity.builder().tankCarNo(tankCarNo).taskType(AddTaskConstant.TASK_TYPE_TQ).build();
        baseMapper.insert(entity);
    }
}
